import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import ContractView from "@/views/ContractView.vue";
import LoginView from "@/views/LoginView.vue";
import OrderView from "@/views/OrderView.vue";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'home',
        component: HomeView,
        children: [
            {
                path: 'contract',
                name: 'contract',
                component: ContractView,
            },
            {
                path: 'order',
                name: 'order',
                component: OrderView,
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: LoginView
    }
]

const router = new VueRouter({
    mode: 'history',
    routes
})
// 导航守卫
router.beforeEach((to, from, next) => {
    let isAuthenticated = sessionStorage.getItem("token");//获取token
    console.log(isAuthenticated + '-----MyToken-----')
    if (to.name !== 'login' && !isAuthenticated) {//如果没有登录且目标路由不是登录则导航到登录页面
        // next({name: 'login'})
    }
    next();

})
export default router
